package com.hnkywl.sts.action.organ;

import com.hnkywl.sts.entity.organ.Bmfw;
import com.hnkywl.sts.service.sys.BmfwService;
import com.hnkywl.sts.service.sys.YgService;
import net.ruixin.web.struts2.SimpleActionSupport;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * User: ccr
 * Date: 13-5-20
 * Time: 下午8:12
 * To change this template use File | Settings | File Templates.
 */
@Namespace("/organ/bmfw")
public class BmfwAction extends SimpleActionSupport{
    private BmfwService bmfwService;
    private Long gsId;
    private Long ygId;
    private int type;
    private String code;
    private Long wzbmId;
    private int page;
    private int rows;
    private List<Bmfw> bmfwList;

    @Action("bmfwSave")
    public void save(){
        try{
            bmfwService.save(bmfwList);
            renderText("{\"statusCode\":200}");
        }catch (Exception  e) {
            e.printStackTrace();
            renderText("{\"statusCode\":300,\"message\":\""+e.getMessage()+"\"}");
        }
    }

    @Action("bmfwedit")
    public String bmfwedit(){
        return SUCCESS;
    }

    @Action("bmfw")
    public String bmfw(){
        return SUCCESS;
    }

    @Action("get_treejson")
    public void getTreeJson() {
        if(gsId == null){
            gsId = YgService.getCurrentYgGsId();
        }
        if (request.getParameter("id") == null) {
            StringBuilder sb = new StringBuilder("[{\"id\": \"0:0:0\", \"text\": \"编码物资\", \"attributes\": {\"level\": \"0\", \"children\": \"false\"}, \"state\": \"open\",\"children\":");
            String treeJson = bmfwService.getWzbmTreeJson(gsId,0l, 1);
            sb.append(treeJson).append("}]");
            renderJson(sb.toString());
        } else {
            String[] values = request.getParameter("id").split(":");
            wzbmId = Long.valueOf(values[0]);
            type = Integer.valueOf(values[1]);
            gsId = Long.valueOf(values[2]);
            renderJson(bmfwService.getWzbmTreeJson(gsId,wzbmId, type+1));
        }
    }


    @Action("get_treejsonbyyg")
    public void getTreeJsonByYg() {
        if (request.getParameter("id") == null) {
            StringBuilder sb = new StringBuilder("[{\"id\": \"0:0\", \"text\": \"编码物资\", \"attributes\": {\"level\": \"0\", \"children\": \"false\"}, \"state\": \"open\",\"children\":");
            String treeJson = bmfwService.getWzbmTreeJsonByYg(ygId,null, 1);
            sb.append(treeJson).append("}]");
            renderJson(sb.toString());
        } else {
            String[] values = request.getParameter("id").split(":");
            wzbmId = Long.valueOf(values[0]);
            type = Integer.valueOf(values[1]);
            renderJson(bmfwService.getWzbmTreeJsonByYg(ygId,wzbmId, type+1));
        }
    }

    public Long getYgId() {
        return ygId;
    }

    public void setYgId(Long ygId) {
        this.ygId = ygId;
    }

    public Long getGsId() {
        return gsId;
    }

    public void setGsId(Long gsId) {
        this.gsId = gsId;
    }

    public int getType() {
        return type;
    }

    public void setType(int type) {
        this.type = type;
    }

    public Long getWzbmId() {
        return wzbmId;
    }

    public void setWzbmId(Long wzbmId) {
        this.wzbmId = wzbmId;
    }

    public List<Bmfw> getBmfwList() {
        return bmfwList;
    }

    public void setBmfwList(List<Bmfw> bmfwList) {
        this.bmfwList = bmfwList;
    }

    public String getCode() {
        return code;
    }

    public void setCode(String code) {
        this.code = code;
    }

    public int getPage() {
        return page;
    }

    public void setPage(int page) {
        this.page = page;
    }

    public int getRows() {
        return rows;
    }

    public void setRows(int rows) {
        this.rows = rows;
    }

    @Autowired
    public void setBmfwService(BmfwService bmfwService) {
        this.bmfwService = bmfwService;
    }
}
